package com.dongbao.action;

import com.dongbao.dialog.DHGToolWindow;
import com.dongbao.entity.AwardedInfo;
import com.dongbao.entity.CodeScoreInfo;
import com.dongbao.service.CodeGradeBiz;
import com.dongbao.util.ToolWindowUtil;
import com.intellij.notification.*;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.progress.Task;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiFile;
import com.intellij.psi.util.PsiUtilCore;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.NotNull;

import java.util.List;

import static com.dongbao.common.AuthorityEnum.AUTHORITY_NOTIFICATION_TITLE;

/**
 * 打开文件右键菜单面板-检测当前文件动作入口
 * */
public class TestCurentFile extends AnAction {

    private static final Logger LOG = Logger.getInstance(TestCurentFile.class);

    private static final NotificationGroup NOTIFICATION_GROUP = new NotificationGroup(AUTHORITY_NOTIFICATION_TITLE, NotificationDisplayType.BALLOON, true);

    private final CodeGradeBiz codeGradeBiz = new CodeGradeBiz();

    @Override
    public void actionPerformed(AnActionEvent e) {
        /* 调用保存方法保存文件 */
        FileDocumentManager.getInstance().saveAllDocuments();
        ProgressManager.getInstance().run(new Task.Backgroundable(e.getProject(), e.getPresentation().getText(), false) {
            @Override
            public void run(@NotNull ProgressIndicator indicator) {
                ApplicationManager.getApplication().runReadAction(
                        () -> {
                            runCode(e);
                        }
                );
            }
        });

    }

    public void runCode(AnActionEvent e){


        /* 获取当前选择的文件 */
        VirtualFile virtualFile = e.getRequiredData(CommonDataKeys.VIRTUAL_FILE);
        /* 执行代码检测评分 */
        CodeScoreInfo codeScoreInfo = new CodeScoreInfo();
        PsiFile psiFile = PsiUtilCore.getPsiFile(e.getProject(), virtualFile);
        CodeScoreInfo calculate = codeGradeBiz.calculate(virtualFile,psiFile, codeScoreInfo,e.getProject());
        /* 分析评分并将错误原因弹出 */
        /* 遍历得分情况 进行分数的加减 */
        List<AwardedInfo> deductions = codeScoreInfo.getDeduction();
        codeGradeBiz.score(deductions,codeScoreInfo);
        /* 修改代码提交时的评论,追加代码评分情况上去 */
//        IdeaApiUtil.showNotification("您最终提交代码得分:"+allScoreInfo.getScore()+"<br/><a>查看扣分项</a>", NotificationType.INFORMATION,Datacenter.project);

        DHGToolWindow fastRequestToolWindow = ToolWindowUtil.getDHGToolWindow(e.getProject());
        fastRequestToolWindow.refreshErrorInfos(codeScoreInfo);

        NOTIFICATION_GROUP.createNotification("您最终提交代码得分:"+codeScoreInfo.getScore(), NotificationType.INFORMATION)
                .addAction(new NotificationAction("查看扣分项") {
                    @Override
                    public void actionPerformed(@NotNull AnActionEvent e, @NotNull Notification notification) {
                        /* 跳转到DHG开发工具栏的代码检测 */
                        if (fastRequestToolWindow != null) {
                            fastRequestToolWindow.skipCodeInfo();
                        }
                    }
                })
                .notify(e.getProject());
        LOG.info("执行代码检测");
    }
}
